package com.bocai.kelalacar.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 作者 yuanfei on 2016/12/6.
 * 邮箱 yuanfei221@126.com
 */

public class DateUtil {

    public static String getTodayDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault());
        return format.format(new Date());
    }

    public static Date getTodayDate() {

        return new Date();
    }

    public static Date setStringToData(String data) {
        //2017-01-05T23:55:00
        Date date = new Date();
        if (data.contains("T")) {
            data = data.replace("T", " ");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        try {
            date = sdf.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 掉此方法输入所要转换的时间输入例如（"2014年06月14日16时09分00秒"）返回时间戳
     *
     * @param time
     * @return
     */
    public static String data(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
                Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            l = l / 1000;
            String stf = String.valueOf(l);
            times = stf;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    public static String dateToDateTimeString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 调用此方法输入所要转换的时间戳输入例如（1402733340）输出（"2014-06-14"）
     *
     * @param time
     * @return
     */
    public static String timedate(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    public static String getData() {
        Date date = new Date();
        String times = null;
        try {
            long l = date.getTime();
            l = l / 1000;
            String stf = String.valueOf(l);
            times = stf;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("times", times);
        return times;
    }

    public static String getMonth(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String s = sdf.format(date);
        return s;
    }
    public static String getDays(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        String s = sdf.format(date);
        return s;
    }

    /*
 * 毫秒转化时分秒毫秒
 */
    public static String formatTime(Long ms) {
        boolean isShow = false;
        List<Integer> list = new ArrayList<>();


        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        if (list != null) {
            list.clear();
        }
        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            isShow = true;
            list.add(1);
            if (day < 10) {
                sb.append("0" + day + "天");
            } else {
                sb.append(day + "天");
            }
        }
        if (((isShow && hour >= 0) || (hour > 0)) && list.size() < 2) {
            isShow = true;
            list.add(1);
            if (hour < 10) {
                sb.append("0" + hour + "时");
            } else {
                sb.append(hour + "时");
            }
        }
        if (((isShow && minute >= 0) || (minute > 0)) && list.size() < 2) {
            isShow = true;
            list.add(1);
            if (minute < 10) {
                sb.append(":" + "0" + minute + "分");
            } else {
                sb.append(":" + minute + "分");
            }
        }
        if (((isShow && second >= 0) || second > 0) && list.size() < 2) {
            list.add(1);
            if (second < 10) {
                sb.append(":" + "0" + second);
            } else {
                sb.append(":" + second);
            }
        }
        return sb.toString();
    }

    public static List<String>  formatTime1(Long ms) {
        List<String> list = new ArrayList<>();


        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        if (list != null) {
            list.clear();
        }
        StringBuffer sb = new StringBuffer();

        list.add(day+"天");

        if (hour < 10) {
            list.add("0" + hour);
        } else {
            list.add(hour + "");
        }


        if (minute < 10) {
            list.add("0" + minute );
        } else {
            list.add(minute + "");
        }


        if (second < 10) {
            list.add("0" + second);
        } else {
            list.add(second+"");

        }
        return list;
    }

    public static String formatTime2(Long ms) {

        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            if (day < 10) {
                sb.append("0" + day + "天");
            } else {
                sb.append(day + "天");
            }
        }
            if (hour < 10) {
                sb.append("0" + hour );
            } else {
                sb.append(hour + "");
            }
            if (minute < 10) {
                sb.append(":" + "0" + minute );
            } else {
                sb.append(":" + minute + "");
            }
            if (second < 10) {
                sb.append(":" + "0" + second);
            } else {
                sb.append(":" + second);
        }
        return sb.toString();
    }

    public static String formatTime3(Long ms) {
        boolean isShow = false;
        List<Integer> list = new ArrayList<>();


        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        if (list != null) {
            list.clear();
        }
        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            isShow = true;
            list.add(1);
            if (day < 10) {
                sb.append("0" + day + "天");
            } else {
                sb.append(day + "天");
            }
        }
        if (((isShow && hour >= 0) || (hour > 0)) && list.size() < 3) {
            isShow = true;
            list.add(1);
            if (hour < 10) {
                sb.append("0" + hour + "时");
            } else {
                sb.append(hour + "时");
            }
        }
        if (((isShow && minute >= 0) || (minute > 0)) && list.size() < 3) {
            isShow = true;
            list.add(1);
            if (minute < 10) {
                sb.append("" + "0" + minute + "分");
            } else {
                sb.append("" + minute + "分");
            }
        }
        if (((isShow && second >= 0) || second > 0) && list.size() < 3) {
            list.add(1);
            if (second < 10) {
                sb.append("" + "0" + second+"秒");
            } else {
                sb.append("" + second+"秒");
            }
        }
        return sb.toString();
    }
    public static String formatTime4(Long ms) {

        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        StringBuffer sb = new StringBuffer();
        if (day > 0) {
                sb.append(day + "天");
        }
        if (hour < 10) {
            sb.append("0" + hour );
        } else {
            sb.append(hour + "");
        }
        if (minute < 10) {
            sb.append(":" + "0" + minute );
        } else {
            sb.append(":" + minute + "");
        }
        if (second < 10) {
            sb.append(":" + "0" + second);
        } else {
            sb.append(":" + second);
        }
        return sb.toString();
    }

}
